<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 12/8/18
 * Time: 5:24 PM
 */

require_once __DIR__.'/vendor/autoload.php';
use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;

$worker = new Worker();

$worker->onWorkerStart = function() {
    // 以websocket协议连接远程websocket服务器
    $context = [
        'ssl' => [
            'allow_self_signed' => true,
            'verify_peer' => false,
            'verify_peer_name' => false
        ]
    ];
    $ws_connection = new AsyncTcpConnection("ws://ws.jinse.com/ws", $context);
    $ws_connection->transport = 'sslv3';
    // 连上后发送hello字符串
    $ws_connection->onConnect = function($connection){
        $connection->send('{"event":"jinse","action":"sub","channel":"jinse_tick_update"}');
    };
    // 远程websocket服务器发来消息时
    $ws_connection->onMessage = function($connection, $data){
        echo "recv: $data\n";
    };
    // 连接上发生错误时，一般是连接远程websocket服务器失败错误
    $ws_connection->onError = function($connection, $code, $msg){
        echo "error: $msg\n";
    };
    // 当连接远程websocket服务器的连接断开时
    $ws_connection->onClose = function($connection){
        echo "connection closed\n";
    };
    // 设置好以上各种回调后，执行连接操作
    $ws_connection->connect();
};

Worker::runAll();